import { fileURLToPath, URL } from 'node:url'
import path from 'path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
//引入svg插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
//mock插件提供方法
import {viteMockServe} from 'vite-plugin-mock'


// https://vite.dev/config/
export default defineConfig(({ command,mode })=>{
  //获取各种环境下的对象的变量
  let env = loadEnv(mode,process.cwd())
  return {
    plugins: [
      vue(),
      vueDevTools(),
      createSvgIconsPlugin({
        // 
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        // 
        symbolId: 'icon-[dir]-[name]',
      }),
      viteMockServe({
        enable: command === 'serve',
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          //sass1.8后已经弃用@import，使用@use导入 
          additionalData: `@use "@/styles/variable.scss" as *;`,
        },
      },
    },
    //代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]:{
          //获取数据的服务器地址设置
          target: env.VITE_SERVE,
          //需要代理跨域
          changeOrigin:true,
          //路径重写 所有 /api 开头的请求会被代理到后端服务器
          rewrite: (path) => path.replace(/^\/api/,'')
        }
      }
    }
  }
})
